package cn.tedu.loding.controller;

import cn.tedu.loding.pojo.dto.ProductAddNewDTO;
import cn.tedu.loding.pojo.dto.ProductUpdateDTO;
import cn.tedu.loding.pojo.vo.ProductItemVO;
import cn.tedu.loding.pojo.vo.ProductListVO;
import cn.tedu.loding.pojo.vo.ProductStandardVO;
import cn.tedu.loding.service.IProductService;
import cn.tedu.loding.web.JsonResult;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理商品相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/products")
@Api(tags = "10.商品管理模块")
public class ProductController {

    @Autowired
    private IProductService productService;

    public ProductController() {
        log.debug("创建控制器对象：productController");
    }

    // http://localhost:9090/products/add-new?name=商品001&description=商品001的简介&sort=199
    @ApiOperation("添加商品")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<String> addNew(@Valid ProductAddNewDTO productAddNewDTO) {
        log.debug("开始处理【添加商品】的请求，参数：{}", productAddNewDTO);
        productService.addNew(productAddNewDTO);
        log.debug("添加商品成功！");
        return JsonResult.ok("添加商品成功!");
    }

    // http://localhost:9090/products/9527/delete
    @ApiOperation("根据id删除商品")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<String> delete(@Range(min = 1, message = "删除商品失败，尝试删除的商品的ID无效！")
                             @PathVariable Long id) {
        log.debug("开始处理【根据id删除商品】的请求，参数：{}", id);
        productService.delete(id);
        return JsonResult.ok("已删除该商品及其属性和图片");
    }

    @ApiOperation("修改商品详情")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<String> updateById(@Valid ProductUpdateDTO productUpdateDTO) {
        log.debug("开始处理【修改商品详情】的请求，新数据：{}", productUpdateDTO);
        productService.updateInfoById(productUpdateDTO);
        return JsonResult.ok("修改商品成功!");
    }

    @ApiOperation("根据id查询商品详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<ProductItemVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询商品详情】的请求，参数：{}", id);
        ProductItemVO list = productService.getStandardById(id);
        return JsonResult.ok(list);
    }


    //localhost:9090/products
    @ApiOperation("商品列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<ProductItemVO>> List(){
        log.debug("开始处理【商品列表】请求");
          List<ProductItemVO>list = productService.list();
        return JsonResult.ok(list);
    }
}
